<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\AdminRole;
use app\admin\model\Role;
use app\admin\model\RoleRule;
use app\admin\model\Rule;
use think\Controller;
use think\Request;

class Rbac extends Common {
    protected $admin;
    protected $request;
    protected $role;
    protected $rule;
    protected $role_rule;
    protected $admin_role;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->admin = new Admin();
        $this->request = Request::instance();
        $this->role=new Role();
        $this->rule=new Rule();
        $this->role_rule=new RoleRule();
        $this->admin_role=new AdminRole();
    }

    public function administrator(){
        $username=input("username",'');
        $status=input("status",'-1');
        $where=[];
        if($username!=""){
            $where['username']=$username;
        }
        if($status>-1){
            $where['status']=$status;
        }
        $data=$this->admin->pageData($where);
//        echo '<pre>';
//        print_r($data);
//        die;
        $this->assign("username",$username);
        $this->assign("status",$status);
        $this->assign("admins",$data);
        return $this->fetch();
    }
    public function addAdministrator()
    {
        //判断当前i请求是get还是Post

        if ($this->request->isGet()) {
            return $this->fetch();
        } else {
            $username = input('username');
            $password = input('password');
            $status = input('status');
            //混淆字符串(密钥|盐)
            $salt = getRandKey();
            //echo $salt;
            //die();
            $data = [
                'username' => $username,
                'password' => md5(md5($password) . $salt),
                'salt' => $salt,
                'status' => $status,
                'login_ip' => $this->request->ip(),
                'login_time' => time(),
                'create_time' => time(),
                'update_time' => time()
            ];

            $res = $this->admin->addData($data);
            if ($res) {
                $this->success('添加成功', 'administrator');
            } else {
                $this->error('添加失败', 'addAdministrator');
            }
        }
    }
    public function editeAdministrator()
    {
        $id = input('admin_id');
        if ($this->request->isGet()) {
            $where = [
                'admin_id' =>$id
            ];
            $node = $this->admin->getOne($where);
            $this->assign("admin", $node);
            return $this->fetch();
        }else{
            $data=[
                'username'=>input('username'),
                'status'=>input('status')
            ];
            $where=[
                'admin_id'=>$id
            ];
            $res=$this->admin->saveData($where,$data);
            if ($res) {
                $this->success('编辑成功', 'administrator');
            } else {
                $this->error('编辑失败', 'editeAdministrator');
            }
        }
    }
    public function delAdministrator(){
       $ids=input('admin_id/a',[]);
       foreach($ids as $id){
           $where=[
               'admin_id'=>$id
           ];
           $res=$this->admin->delData($where);
       }
       if(input('is_row')){
           if($res){
               return json([
                   'status'=>1,
                   'msg'=>'删除成功'
               ]);
           }else{
               return json([
                   'status'=>0,
                   'msg'=>'删除失败'
               ]);
           }
       }else{
           if($res){
               $this->success('删除成功','administrator');
           }else{
               $this->error('删除失败','administrator');
           }
       }

    }

    public function role(){
        $data=$this->role->getAll();
        $this->assign("roles",$data);
        return $this->fetch();
    }
    public function delRole(){
        $id=input('id');
        $where=[
            'id'=>$id
        ];
        $res=$this->role->delData($where);
        if($res){
            return json([
                'status'=>1,
                'msg'=>'删除成功'
            ]);
        }else{
            return json([
                'status'=>0,
                'msg'=>'删除失败'
            ]);
        }

    }
    public function addRole(){
        if($this->request->isGet()){
            return $this->fetch();
        }else{
            $data=[
                'name'=>input('name'),
                'remark'=>input('remark'),
                'status'=>input('status')
            ];
            $res=$this->role->addData($data);
            if($res){
                $this->success('角色添加成功','role');
            }else{
                $this->error('角色添加失败','addrole');
            }
        }

    }
    public function editeRole(){
        $id=input('id');
        if($this->request->isGet()){
            $data=$this->role->getOne(['id'=>$id]);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data=[
                'name'=>input('name'),
                'remark'=>input('remark'),
                'status'=>input('status'),
            ];
            $where=[
                'id'=>$id
            ];
            $res=$this->role->saveData($where,$data);
            if($res){
                $this->success('角色编辑成功','role');
            }else{
                $this->error('角色编辑失败','editeRole');
            }
        }
    }

    public function addRule(){
        if($this->request->isGet()){
            $id=input('id',0);
            $parent_rule=$this->rule->getAll(['pid'=>0]);
            $this->assign('id',$id);
            $this->assign('parent_rule',$parent_rule);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            $data['level']=$data['pid']==0?1:2;
            $res=$this->rule->addData($data);
            if($res){
                $this->success('添加规则成功','rule');
            }else{
                $this->error('添加规则失败','addrule');
            }
        }
    }
    public function Rule(){
        $data=$this->rule->getAll();
        $res=node_merge($data);
//        p($res);
        $this->assign('rules',$res);
        return $this->fetch();
    }
    public function editeRule(){
        if($this->request->isGet()){
            $id=input('id',0);
            $rule=$this->rule->getOne(['id'=>$id]);
            $parent_rule=$this->rule->getAll(['pid'=>0]);
            $this->assign('parent_rule',$parent_rule);
            $this->assign('data',$rule);
            $this->assign('id',$rule['pid']);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            $id=$data['id'];
            unset($data['id']);
            $res=$this->rule->saveData(['id'=>$id],$data);
            if($res){
                $this->success('编辑规则成功','rule');
            }else{
                $this->error('编辑规则失败','editerule?id='.$id);
            }
        }


    }
    public function delRule(){
        $id=input('id');
        $where="id=$id or pid=$id";
        $res=$this->rule->delData($where);
        if($res){
            return json([
                'status'=>1,
                'msg'=>'删除成功'
            ]);
        }else{
            return json([
                'status'=>0,
                'msg'=>'删除失败'
            ]);
        }
    }
    public function resetPwd(){
        if($this->request->isGet()){
            $admin_id=input('admin_id');
            $this->assign('admin_id',$admin_id);
            return $this->fetch();
        }else{
            $admin_id=input('admin_id');
            $password=input('password');
            $admin=$this->admin->getOne([
                'admin_id'=>$admin_id
            ]);
            $data=['password'=>md5(md5($password).$admin['salt'])];
            $where=[
                'admin_id'=>$admin_id
            ];
            $res=$this->admin->save($data,$where);
            if($res){
                $this->success('重置密码成功','administrator');
            }else{
                $this->error('重置密码失败','resetpwd?admin_id='.$admin_id);
            }
        }
    }
    public function access(){
        if($this->request->isGet()){
            $id=input('id');
            $role=$this->role->getOne(['id'=>$id]);
            $this->assign('role',$role);
            $data=$this->rule->getAll();
            $res=node_merge($data);
            $has_rules=$this->role_rule->getAll(['role_id'=>$id],'rule_id');
            $has_rules_one=[];
            foreach($has_rules as $rule){
                $has_rules_one=$rule['rule_id'];
            }
            $this->assign('rules',$res);
            $this->assign("has_rules_one",$has_rules_one);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            $this->role_rule->delData(['role_id'=>$data['id']]);
            foreach ($data['rule'] as $rule){
                $newData=[
                    'role_id'=>$data['id'],
                    'rule_id'=>$rule
                ];
                $res=$this->role_rule->insert($newData);
            }
            if($res){
                $this->success('权限分配成功','role');
            }else{
                $this->error('权限分配失败','access');
            }
        }
    }
    public function distributionRole(){
        if($this->request->isGet()){
            $admin_id=input('admin_id');
            $admin=$this->admin->getOne(['admin_id'=>$admin_id]);
            $this->assign('admin',$admin);

            $has_roles=$this->admin_role->getAll(['admin_id'=>$admin_id],'role_id');
            $has_roles_one=[];
            foreach ($has_roles as $role){
                $has_roles_one[]=$role['role_id'];
            }


            $roles=$this->role->getAll();
            $this->assign('roles',$roles);
            $this->assign('has_roles_one',$has_roles_one);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            foreach($data['roles'] as $role){
                $add_data=[
                    'admin_id'=>$data['admin_id'],
                    'role_id'=>$role
                ];
                $res=$this->admin_role->addData($add_data);
            }
            if($res){
                $this->success('角色分配成功','administrator');
            }else{
                $this->error('角色分配失败','distributionRole');
            }
        }
    }


}